package org.bits.model;

import java.util.*;

import org.bits.data.*;
import org.jutils.Utils;

/*******************************************************************************
 * 
 ******************************************************************************/
public class TestDataAccess implements DataAccessModel
{
    /**  */
    private Product swProduct;
    /**  */
    private Product dataProduct;
    /**  */
    private List<Product> products;
    /**  */
    private List<Finding> findings;
    /**  */
    private List<Issue> issues;
    /**  */
    private User currentUser;

    /***************************************************************************
     * 
     **************************************************************************/
    public TestDataAccess()
    {
        issues = new ArrayList<Issue>();
        findings = new ArrayList<Finding>();
        products = new ArrayList<Product>();
        currentUser = new User();

        currentUser.userName = System.getProperty( "user.name" );
        currentUser.firstName = "Bob";
        currentUser.lastName = "Roberts";

        createDefaultProducts();
        createDefaultFindings();
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private void createDefaultFindings()
    {
        Finding f;

        f = createFinding( 1, "Can't do it", "Tried to do it and couldn't" );
        findings.add( f );

        f = new Finding();
        f.number = 2;
        f.title = "Doesn't do it";
        f.description = "Want it to do it but it doesn't";
        f.versions.add( getRandVersion( swProduct ) );
        f.versions.add( getRandVersion( dataProduct ) );
        f.state = FindingState.ASSIGNED;
        f.creator = currentUser;
        f.assignee = currentUser;
        f.notes.add( new Note( currentUser,
            "I meant \"It should\" instead of \"Want it to\"" ) );
        f.notes.add( new Note( currentUser,
            "Silly SCCB wants me to rewrite the full description: " +
                Utils.NEW_LINE + "It should do it but it doesn't." ) );
        f.notes.add( new Note( currentUser,
            "and here ends the preformatted</pre><b>BOLD!</b><pre>" ) );
        findings.add( f );

        for( int i = 3; i < 40; i++ )
        {
            f = createFinding( i, "Finding # " + i, "Some random problem # " +
                i );
            findings.add( f );
        }
    }

    private Finding createFinding( int num, String title, String desc )
    {
        Finding f = new Finding();

        f.number = num;
        f.title = title;
        f.description = desc;
        f.versions.add( getRandVersion( swProduct ) );
        f.state = FindingState.NEW;
        f.creator = currentUser;

        return f;
    }

    private Version getRandVersion( Product product )
    {
        Random rand = new Random();
        List<Release> releases = getReleases( product );
        Release r = releases.get( rand.nextInt( releases.size() ) );
        List<String> builds = getBuilds( r );
        String b = builds.get( rand.nextInt( builds.size() ) );

        return new Version( product, r, b );
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private void createDefaultProducts()
    {
        swProduct = new Product( "Software" );
        dataProduct = new Product( "Data" );

        products.add( swProduct );
        products.add( dataProduct );
    }

    /***************************************************************************
     * 
     **************************************************************************/
    @Override
    public List<Product> getProducts()
    {
        return products;
    }

    /***************************************************************************
     * 
     **************************************************************************/
    @Override
    public List<Release> getReleases( Product product )
    {
        List<Release> versions = new ArrayList<Release>();

        if( product.name.equals( swProduct.name ) )
        {
            Release v;

            v = new Release( "1", DevelopmentState.FQT );
            versions.add( v );

            v = new Release( "2", DevelopmentState.DEVELOPMENT );
            versions.add( v );
        }
        else if( product.name.equals( dataProduct.name ) )
        {
            Release v;

            v = new Release( "A", DevelopmentState.FQT );
            versions.add( v );

            v = new Release( "B", DevelopmentState.DEVELOPMENT );
            versions.add( v );
        }

        return versions;
    }

    /***************************************************************************
     * 
     **************************************************************************/
    @Override
    public List<String> getBuilds( Release r )
    {
        List<String> builds = new ArrayList<String>();

        builds.add( "34" );
        builds.add( "35" );
        builds.add( "36" );

        return builds;
    }

    /***************************************************************************
     * 
     **************************************************************************/
    @Override
    public List<Issue> filterIssues( Filter<Issue> filter )
    {
        // TODO Auto-generated method stub
        return null;
    }

    /***************************************************************************
     * 
     **************************************************************************/
    @Override
    public List<Finding> filterFindings( Filter<Finding> filter )
    {
        return findings;
    }

    /***************************************************************************
     * 
     **************************************************************************/
    @Override
    public List<Task> filterTasks( Filter<Task> filter )
    {
        // TODO Auto-generated method stub
        return null;
    }

    /***************************************************************************
     * 
     **************************************************************************/
    @Override
    public List<Issue> filterAuthorizations( Filter<Issue> filter )
    {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public User getCurrentUser()
    {
        return null;
    }

    @Override
    public void addIssue( Issue issue )
    {
        issue.number = issues.size() + 1;
        issues.add( issue );
    }

    @Override
    public void addFinding( Finding finding )
    {
        finding.number = findings.size() + 1;
        finding.creationDate = new Date().getTime();
        finding.creator = currentUser;
        finding.state = FindingState.NEW;
        findings.add( finding );
    }
}
